package com.asia5b.a5mall.middleware.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.view.animation.AnimationSet
import android.view.animation.TranslateAnimation
import io.reactivex.Observable
import java.io.IOException
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   App通用工具类
 */
object AppTool {

    /**
     * @Note   动画工具类
     */
    object Anim {

        /**
         * @Note   构建popwindow进入动画
         * @param  context 上下文
         * @param  fromYDelta 开始y点
         * @param  duration 动画时间
         */
        @JvmStatic
        fun createPopInAnimation(context: Context, fromYDelta: Float, duration: Long): Animation {
            val set = AnimationSet(context, null)
            set.setFillAfter(true)
            val animation = TranslateAnimation(0f, 0f, fromYDelta, 0f)
            animation.setDuration(duration)
            set.addAnimation(animation)
            val alphaAnimation = AlphaAnimation(0f, 1f)
            alphaAnimation.setDuration(duration)
            set.addAnimation(alphaAnimation)
            return set
        }

        /**
         * @Note   构建popwindow进入动画
         * @param  context 上下文
         * @param  toYDelta 结束y点
         * @param  duration 动画时间
         */
        @JvmStatic
        fun createPopOutAnimation(context: Context, toYDelta: Float, duration: Long): Animation {
            val set = AnimationSet(context, null)
            set.setFillAfter(true)
            val animation = TranslateAnimation(0f, 0f, 0f, toYDelta)
            animation.duration = duration
            set.addAnimation(animation)
            val alphaAnimation = AlphaAnimation(1f, 0f)
            alphaAnimation.duration = duration
            set.addAnimation(alphaAnimation)
            return set
        }
    }





    /**
     * @Note   图片工具类
     */
    object Image {

        /**
         * @Note   url获取Bitmap
         * @param  url 图片URL
         */
        @JvmStatic
        fun urlToBitmap(url: String): Observable<Bitmap> {
            return Observable.create { subscriber ->
                var fileUrl: URL? = null
                val bitmap: Bitmap

                try {
                    fileUrl = URL(url)
                } catch (e: MalformedURLException) {
                    subscriber.onError(e)
                }


                try {
                    val conn = fileUrl!!.openConnection() as HttpURLConnection
                    conn.doInput = true
                    conn.connect()
                    val inStream = conn.inputStream
                    bitmap = BitmapFactory.decodeStream(inStream)
                    inStream.close()
                    subscriber.onNext(bitmap)
                } catch (e: IOException) {
                    subscriber.onError(e)
                }
            }
        }
    }




}